#include "mcs51/8052.h"
#include "../lib/lcd/lcd1602.h"

#define key1 P3_1
#define key2 P3_0
#define key3 P3_2
#define key4 P3_3
#define LED1 P2_0

#define LSA P2_2
#define LSB P2_3
#define LSC P2_4

void light1() {
    P2_0 = 0;
    while (1) {

    }
}
//void delay_ms(unsigned int delayms) {
//    unsigned int delayus = delayms * 1000;
//    //一个循环10us
//    unsigned int delayunit = delayus / 10;
//    while (delayunit--);
//}
/**
 * 按键控制灯
 * @param scanmode 0:扫描1次 1:无数次
 */
void led_key_board(int scanmode) {
    int scan = 1;
    while (scan) {
        if (key1 == 0 || key2 == 0 || key3 == 0 || key4) {
            //消抖10ms
            delay_ms(10);

            if (key1 == 0) {
                //按k1就控制灯1
                LED1 = !LED1;
            }
            if (key2 == 0) {
                //二进制显示1111 1111
                P2++;
            }
            if (key3 == 0) {

            }
            if (key4 == 0) {

            }
        }
        scan = scanmode;
    }
}

/**
 * 按键右向或者左向流水
 */
void led_right_key_board() {
    unsigned int kbp_times = 0;

    //上电第一个亮起
    P2 = ~(0x01);
    while (1) {
        // 方向<<<<<<
        if (key1 == 0) {
            //消除抖动
            delay_ms(10);
            if (key1 == 0) {
                //按键一次，向左边移动一次
                kbp_times++;
                if (kbp_times >= 8) {
                    kbp_times = 0;
                }
                P2 = ~(0x01 << kbp_times);
            }
        }

        // 方向>>>>>>
        if (key2 == 0) {
            //消除抖动
            delay_ms(10);
            if (key2 == 0) {
                //按键一次，向右边移动一次
                if (kbp_times == 0) {
                    kbp_times = 7;
                } else {
                    kbp_times--;
                }
                P2 = ~(0x01 << kbp_times);
            }
        }
    }
}

/**
 * 数码管
 * @param pos 位
 * @param num 显示数字
 */
void smg_show(unsigned int pos, unsigned int num) {
    switch (pos) {
        //位选
        case 0:
            LSC = 1;
            LSB = 1;
            LSA = 1;
            break;
        case 1:
            LSC = 1;
            LSB = 1;
            LSA = 0;
            break;
        case 2:
            LSC = 1;
            LSB = 0;
            LSA = 1;
            break;
        case 3:
            LSC = 1;
            LSB = 0;
            LSA = 0;
            break;
        case 4:
            LSC = 0;
            LSB = 1;
            LSA = 1;
            break;
        case 5:
            LSC = 0;
            LSB = 1;
            LSA = 0;
            break;
        case 6:
            LSC = 0;
            LSB = 0;
            LSA = 1;
            break;
        case 7:
            LSC = 0;
            LSB = 0;
            LSA = 0;
            break;
    }
    //段选
    char smgcode[17] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
                        0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
    P0 = smgcode[num];
    delay_ms(1);
    P0 = 0x00;
}

int main() {
    return 1;
}
